#!/bin/bash

# 定义颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NC='\033[0m' # 无颜色

# 动态打印函数
print_with_delay() {
  local text="$1"
  local delay=0.02
  for ((i=0; i<${#text}; i++)); do
    echo -n "${text:$i:1}"
    sleep $delay
  done
  echo
}

# 打印分隔线
print_separator() {
  echo -e "${YELLOW}--------------------------------------${NC}"
}

# 路径变量定义
folder_a="/usr/share/X11/xorg.conf.d"
file_b="xorg.conf"
path_c="/home/root123/CONFIG"

# 开始打印
print_with_delay "进入到巡逻车配置程序"
print_separator

# 检查并配置 VEHICLE_UID
configure_vehicle_uid() {
  if grep -q "export VEHICLE_UID=" ~/.bashrc; then
    current_uid=$(grep "export VEHICLE_UID=" ~/.bashrc | cut -d'=' -f2)
    echo -e "检测到UID已经配置为 ${GREEN}$current_uid${NC}"
    while true; do
      echo -e "${GREEN}[1]: 重新配置${NC}"
      echo -e "${GREEN}[2]: 进入下一项配置${NC}"
      read -p "请选择: " choice
      if [ "$choice" == "1" ]; then
        read -p "请输入新的UID后按下回车: " new_uid
        sed -i "/export VEHICLE_UID=/c\export VEHICLE_UID=$new_uid" ~/.bashrc
        echo -e "VEHICLE_UID已更新为 ${GREEN}$new_uid${NC} 并保存到 .bashrc"
        break
      elif [ "$choice" == "2" ]; then
        break
      else
        echo -e "${RED}输入的指令无效，请重新选择。${NC}"
      fi
    done
  else
    echo -e "${RED}检测到UID未配置${NC}"
    read -p "请输入UID后按下回车: " new_uid
    echo "export VEHICLE_UID=$new_uid" >> ~/.bashrc
    echo -e "VEHICLE_UID已添加到 .bashrc"
  fi
  print_with_delay "***配置完成，进入到底盘型号的配置中***"
  print_separator
}

# 检查并配置 底盘型号
configure_chassis_model() {
  if grep -q "export CHASSIS_MODEL=" ~/.bash_profile; then
    current_account=$(grep "export CHASSIS_MODEL=" ~/.bash_profile | cut -d'=' -f2)
    echo -e "检测到底盘型号已经配置为 ${GREEN}$current_account${NC}"
    while true; do
      echo -e "${GREEN}[1]: 重新配置${NC}"
      echo -e "${GREEN}[2]: 进入下一项配置${NC}"
      read -p "请选择: " choice
      if [ "$choice" == "1" ]; then
        echo -e "${GREEN}[1]: RuiMove${NC}"
        echo -e "${GREEN}[2]: Yuhesen${NC}"
        read -p "请选择底盘型号: " model_choice
        if [ "$model_choice" == "1" ]; then
          new_account="RuiMove"
        elif [ "$model_choice" == "2" ]; then
          new_account="Yuhesen"
        else
          echo -e "${RED}输入的指令无效，请重新选择。${NC}"
          continue
        fi
        sed -i "/export CHASSIS_MODEL=/c\export CHASSIS_MODEL=$new_account" ~/.bash_profile
        echo -e "底盘型号已更新为 ${GREEN}$new_account${NC} 并保存到 .bash_profile"
        break
      elif [ "$choice" == "2" ]; then
        break
      else
        echo -e "${RED}输入的指令无效，请重新选择。${NC}"
      fi
    done
  else
    echo -e "${RED}检测到底盘型号未配置${NC}"
    echo -e "${GREEN}[1]: RuiMove${NC}"
    echo -e "${GREEN}[2]: banana${NC}"
    read -p "请选择底盘型号: " model_choice
    if [ "$model_choice" == "1" ]; then
      new_account="RuiMove"
    elif [ "$model_choice" == "2" ]; then
      new_account="Yuhesen"
    else
      echo -e "${RED}输入的指令无效，请重新选择。${NC}"
      return
    fi
    echo "export CHASSIS_MODEL=$new_account" >> ~/.bash_profile
    echo -e "底盘型号已添加到 .bash_profile"
  fi
  print_with_delay "***配置完成，进入到NTRIP账户的配置中***"
  print_separator
}


# 检查并配置 NTRIP 账号
configure_ntrip_username() {
  if grep -q "export NTRIP_USERNAME=" ~/.bash_profile; then
    current_account=$(grep "export NTRIP_USERNAME=" ~/.bash_profile | cut -d'=' -f2)
    echo -e "检测到NTRIP账号已经配置为 ${GREEN}$current_account${NC}"
    while true; do
      echo -e "${GREEN}[1]: 重新配置${NC}"
      echo -e "${GREEN}[2]: 进入下一项配置${NC}"
      read -p "请选择: " choice
      if [ "$choice" == "1" ]; then
        read -p "请输入新的NTRIP账号后按下回车: " new_account
        sed -i "/export NTRIP_USERNAME=/c\export NTRIP_USERNAME=$new_account" ~/.bash_profile
        echo -e "NTRIP账号已更新为 ${GREEN}$new_account${NC} 并保存到 .bash_profile"
        break
      elif [ "$choice" == "2" ]; then
        break
      else
        echo -e "${RED}输入的指令无效，请重新选择。${NC}"
      fi
    done
  else
    echo -e "${RED}检测到NTRIP账号未配置${NC}"
    read -p "请输入NTRIP账号后按下回车: " new_account
    echo "export NTRIP_USERNAME=$new_account" >> ~/.bash_profile
    echo -e "NTRIP账号已添加到 .bash_profile"
  fi
  print_with_delay "***配置完成，进入到NTRIP密码的配置中***"
  print_separator
}

# 检查并配置 NTRIP 密码
configure_ntrip_password() {
  if grep -q "export NTRIP_PASSWORD=" ~/.bash_profile; then
    current_password=$(grep "export NTRIP_PASSWORD=" ~/.bash_profile | cut -d'=' -f2)
    print_with_delay "检测到NTRIP密码已配置"
    while true; do
      echo -e "${GREEN}[1]: 重新配置${NC}"
      echo -e "${GREEN}[2]: 完成配置${NC}"
      read -p "请选择: " choice
      if [ "$choice" == "1" ]; then
        read -p "请输入新的NTRIP密码后按下回车: " new_password
        sed -i "/export NTRIP_PASSWORD=/c\export NTRIP_PASSWORD=$new_password" ~/.bash_profile
        print_with_delay "NTRIP密码已更新并保存到 .bash_profile"
        break
      elif [ "$choice" == "2" ]; then
        break
      else
        echo -e "${RED}输入的指令无效，请重新选择。${NC}"
      fi
    done
  else
    echo -e "${RED}检测到NTRIP密码未配置${NC}"
    read -p "请输入NTRIP密码后按下回车: " new_password
    echo "export NTRIP_PASSWORD=$new_password" >> ~/.bash_profile
    print_with_delay "NTRIP密码已添加到 .bash_profile"
  fi
  print_with_delay "***配置完成，进入到虚拟屏幕的配置中***"
  print_separator
}

# 配置虚拟屏幕
configure_virtual_screen() {
  while true; do
    print_with_delay "请选择是否需要配置虚拟屏幕:"
    echo -e "${GREEN}[1]: 不需要配置虚拟屏幕${NC}"
    echo -e "${GREEN}[2]: 需要配置虚拟屏幕${NC}"
    read -p "请选择: " choice
    if [ "$choice" == "1" ]; then
      # 如果文件存在，则删除该文件
      if [ -f "$folder_a/$file_b" ];then
        print_with_delay "发现文件 $file_b。正在删除..."
        sudo rm -rf "$folder_a/$file_b"
        print_with_delay "删除完成。"
      else
        print_with_delay "已完成配置"
      fi
      break
    elif [ "$choice" == "2" ];then
      # 如果文件不存在，则从路径c拷贝到路径a
      if [ ! -f "$folder_a/$file_b" ];then
        print_with_delay "正在从 $path_c 拷贝..."
        sudo cp "$path_c/$file_b" "$folder_a"
        print_with_delay "拷贝完成。"
      else
        print_with_delay "已完成配置"
      fi
      break
    else
      echo -e "${RED}输入的指令无效，请重新选择。${NC}"
    fi
  done
  print_with_delay "***虚拟屏幕配置完成***"
  print_separator
}

# 配置SSH免密登录
configure_ssh_key() {
  REMOTE_USER="root" 
  REMOTE_HOST="114.80.36.225" 
  REMOTE_PASSWORD="gmkj@0505"
  SSH_KEY_PATH="$HOME/.ssh/id_rsa"

  # 生成SSH密钥对
  if [ ! -f "$SSH_KEY_PATH" ]; then
    echo "Generating SSH key..."
    ssh-keygen -t rsa -b 4096 -f "$SSH_KEY_PATH" -N ""
  else
    echo "SSH key already exists at $SSH_KEY_PATH"
  fi

  # 安装sshpass，如果没有安装的话
  if ! command -v sshpass &> /dev/null; then
    echo "sshpass could not be found, installing it..."
    sudo apt-get update && sudo apt-get install -y sshpass
  fi

  # 检查是否已经配置了免密登录
  if ssh -o PasswordAuthentication=no "$REMOTE_USER@$REMOTE_HOST" "echo 'SSH key is already installed'"; then
    echo -e "${GREEN}SSH免密配置成功${NC}"
  else
    # 复制公钥到远程服务器
    echo "Copying SSH key to remote server..."
    sshpass -p "$REMOTE_PASSWORD" ssh-copy-id -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST"

    # 测试免密登录
    echo "Testing passwordless SSH login..."
    if ssh -o PasswordAuthentication=no "$REMOTE_USER@$REMOTE_HOST" "echo 'Passwordless SSH login successful!'"; then
      echo -e "${GREEN}SSH免密配置成功!.${NC}"
    else
      echo -e "${RED} SSH免密登陆配置失败! 请确保网络连接正常，然后再次尝试，如果依然失败，请联系技术人员.${NC}"
    fi
  fi
  print_with_delay "***结束SSH免密登录配置***"
  print_separator
}

# 配置 /mnt
configure_mnt() {
  print_with_delay "开始配置 /mnt 文件..."
  # 确保环境变量已加载到当前脚本中
  source ~/.bashrc
  # 打印 VEHICLE_UID 以确认其值
  echo "VEHICLE_UID: $VEHICLE_UID"
  # 直接在当前脚本中执行 envsubst
  envsubst < /mnt/autodrive/config/bussiness_t.json > /mnt/autodrive/config/bussiness.json
  echo -e "${GREEN}/mnt 配置完成${NC}"
  print_separator
}
# 配置 npc
# 配置 npc
configure_npc() {
  print_with_delay "开始安装 npc..."
  print_separator
  source ~/.bashrc
  # 安装 npc
  print_with_delay "[config] 配置 npc..."
  sudo ~/bin/linux_amd64_client/npc install -server=114.80.36.225:8024 -vkey=${VEHICLE_UID} -type=tcp
  if [ $? -ne 0 ]; then
    echo -e "${RED}npc配置失败，请重新配置，如果依然失败，请联系技术人员。${NC}"
    print_separator
    return 1
  fi
  sudo npc start
  if [ $? -ne 0 ]; then
    echo -e "${RED}npc启动失败，请重新配置，如果依然失败，请联系技术人员。${NC}"
    print_separator
    return 1
  fi

  echo -e "${GREEN}npc 配置完成。${NC}"
  print_separator
}
 
# 主脚本执行
configure_vehicle_uid
configure_chassis_model
configure_ntrip_username
configure_ntrip_password
configure_virtual_screen
configure_ssh_key
configure_mnt
configure_npc

